package com.gears_software.sxdc.dccx.ui.activity;

import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.gears_software.sxdc.dccx.R;
import com.gears_software.sxdc.dccx.application.DCCXApplication;
import com.gears_software.sxdc.dccx.data.finder.IMemberFinder;
import com.gears_software.sxdc.dccx.data.finder.IRidingTimeFinder;
import com.gears_software.sxdc.dccx.data.model.MemberModel;
import com.gears_software.sxdc.dccx.data.model.ResultModel;
import com.gears_software.sxdc.dccx.data.model.request.RRidingTimeModel;
import com.sdsmdg.tastytoast.TastyToast;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.App;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.rest.spring.annotations.RestService;

import qiu.niorgai.StatusBarCompat;

/**
 * Created by SunMuran on 2017/7/28.
 * 登记乘客信息
 */
@EActivity(R.layout.register_riding_information)
public class RegisterRidingInformationActivity extends Activity {

    @App
    DCCXApplication app;
    @ViewById
    TextView actionbar_title;
    @ViewById
    TextView back_icon;
    @ViewById
    TextView right_icon;
    @ViewById
    EditText et_shangban_date;
    @ViewById
    EditText et_xiaban_date;
    @ViewById
    Button btn_submit;
    @ViewById
    Button btn_cancel;
    @RestService
    IRidingTimeFinder finder;
    RRidingTimeModel model = new RRidingTimeModel();
    @RestService
    IMemberFinder memberFinder;
    MemberModel memberModel;

    @AfterViews
    void created() {
        actionbar_title.setText("登记乘车信息");
        initIconfont();
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(this, R.color.colorActionbar));

        getCurrentMember();
    }

    void initIconfont() {
        back_icon.setTypeface(app.iconfont);
        back_icon.setText(R.string.back_icon);
        right_icon.setVisibility(View.GONE);
    }

    @Click
    void btn_submit() {
        if (et_shangban_date.getText().toString().equals("")) {
            TastyToast.makeText(this, "请输入上班时间", app.SHORT, app.ERROR);
        } else if (et_xiaban_date.getText().toString().equals("")) {
            TastyToast.makeText(this, "请输入下班时间", app.SHORT, app.ERROR);
        } else if (!et_shangban_date.getText().toString().matches("([01]\\d|2[0-4]):[0-5]\\d")){
            TastyToast.makeText(this, "请输入上班时间正确的格式", app.SHORT, app.ERROR);
        } else if (!et_xiaban_date.getText().toString().matches("([01]\\d|2[0-4]):[0-5]\\d")) {
            TastyToast.makeText(this, "请输入下班时间正确的格式", app.SHORT, app.ERROR);
        } else {
            model.setDefaultonwork(et_xiaban_date.getText().toString().trim());
            model.setDefaultoffwork(et_shangban_date.getText().toString().trim());
            UpdateRidingTime(model);
        }
    }


    @Background
    void UpdateRidingTime(RRidingTimeModel model) {
        ResultModel<String> res = finder.updateRidingTime(model);
        showResout(res);
    }

    @UiThread
    void showResout(ResultModel<String> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                TastyToast.makeText(this, "修改乘车信息成功", app.SHORT, app.SUCCESS);
                this.finish();
            } else {
                TastyToast.makeText(this, "修改乘车信息成功", app.SHORT, app.ERROR);
            }
        }
    }

    @Background
    void getCurrentMember() {
        ResultModel<MemberModel> result = memberFinder.getMember();
        showMemberResult(result);
    }

    @UiThread
    void showMemberResult(ResultModel<MemberModel> res) {
        if (res != null) {
            if (res.getErrno().equals("0")) {
                if (res.getData() != null) {
                    memberModel = res.getData();
                    if (memberModel.getDefaultoffwork() != null && !memberModel.getDefaultoffwork().equals("")) {
                        et_shangban_date.setText(memberModel.getDefaultoffwork() + "");
                    }
                    if (memberModel.getDefaultonwork() != null && !memberModel.getDefaultonwork().equals("")) {
                        et_xiaban_date.setText(memberModel.getDefaultonwork() + "");
                    }
                }
            }
        }
    }


    @Click
    void btn_cancel() {
        finish();
    }

    @Click
    void back_icon() {
        finish();
    }
}
